package com.gmail.ceac13;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity {
	protected static final int LOGIN = 1;
	public static final int MENU_CONFIG = 10;
	SharedPreferences settings;
	Button entrar;
	Button novoUsuario;
	TextView login;
	TextView senha;
	boolean resultadoLogin = false;
	ProgressDialog pdialog;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        
        
        settings = getSharedPreferences("ConnectMe",MODE_PRIVATE);
        
        int user_id = settings.getInt("user_id", -1);
        if (user_id > -1) {
        	Intent intent = new Intent(LoginActivity.this, ConnectMEActivity.class);
			startActivity(intent);
        }
        
       	this.entrar = (Button) this.findViewById(R.id.botaoEntrar);
		this.entrar.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				pdialog = ProgressDialog.show(LoginActivity.this, null, "Verificando usuário e senha...");
				validarLogin();
			}
		});
		this.novoUsuario = (Button) findViewById(R.id.botaoNovoUsuario);
		this.novoUsuario.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(LoginActivity.this, NovoUsuarioActivity.class);
				startActivity(intent);
			}
		});
	}
	private void validarLogin() { //Thread
		new Thread() {
			@Override
			public void run() {
				try {
					SharedPreferences settings = getSharedPreferences("ConnectMe",MODE_PRIVATE);
			    	String endServer = settings.getString("servidor", "http://connectme.scienceontheweb.net/");
					login = (TextView) findViewById(R.id.usuario);
					senha = (TextView) findViewById(R.id.senha);
					
					if (login.getText().toString().equals("adm") && senha.getText().toString().equals("adm")) {
						//chamar activity
						SharedPreferences.Editor editor = settings.edit();
						editor.putInt("user_id", 0);
						editor.putString("user_name", "Administrador");
						editor.commit();
						resultadoLogin = true;
					}
					else if (Servidor.isLogin(login.getText().toString(), senha.getText().toString(), endServer) > -1) {
						SharedPreferences.Editor editor = settings.edit();
						int user_id = Servidor.isLogin(login.getText().toString(), senha.getText().toString(), endServer);
						editor.putInt("user_id", user_id);
						editor.putString("user_name", Servidor.buscarUsuario(user_id, endServer).getNome());
						editor.commit();
						
						resultadoLogin = true;
					}
					else {
						//Alerta erro
						Toast t = Toast.makeText(LoginActivity.this, "Login ou senha Invalidos.", Toast.LENGTH_LONG);
						t.show();
						resultadoLogin = false;
					}
					executar();
				} catch(Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
	
	private void executar() {
		//ConnectMEActivity.dialog.dismiss();
		pdialog.dismiss();
		if (resultadoLogin) {
			resultadoLogin = false;
			Intent intent = new Intent(LoginActivity.this, ConnectMEActivity.class);
			startActivity(intent);
		}
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
    	//Configurações
    	//Atualizar

    	menu.add(Menu.NONE, MENU_CONFIG, Menu.NONE, "Configuracoes");
    	return(super.onCreateOptionsMenu(menu));
    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {    	
    	case MENU_CONFIG:
    		Intent intentConf = new Intent(this, ConfiguracoesActivity.class);
			startActivity(intentConf);
    	return true;
    	default:
    	return super.onOptionsItemSelected(item);
    	}
   	}
	
}
